import openpyxl
import sys
from openpyxl.utils.cell import(
    coordinate_to_tuple
)

def doSum(rows):
    result = 0

    for row in rows:
        result += row[0].value

    return result

def doAvg(rows):

    result = 0

    count = 0
    for row in rows:
        result += row[0].value
        count += 1
    result = result / count

    return result

filePath = sys.argv[1]
#params = 'A1:A5000|SUM,B1:B5000|AVG'
params = sys.argv[2]

wb = openpyxl.load_workbook(filePath)
sheet = wb['Sheet1']

# command example: 
#'A1:A5000|SUM' 
# from ['A1:A5000|SUM','B1:B5000|AVG']
for command in params.split(','):
	# commandParams example: 
	# 'A1:A5000|SUM' to ['A1:A5000','SUM']
    commandParams = command.split('|')

	# coordinates example: 
	# 'A1:A5000' to ['A1','A5000']
    coordinates = commandParams[0].split(':')
    # cstart example: 'A1' to (1,1)
    cstart = coordinate_to_tuple(coordinates[0])
	# cend example: 'A5000' to (5000,1)
    cend = coordinate_to_tuple(coordinates[1])

    # ### 2.1
    rows = sheet.iter_rows(
        min_row = cstart[0], # 1
        max_row = cend[0], # 5000

        min_col = cstart[1], # 1
        max_col = cend[1] # 1
    )

    # ### 2.2
    commandAction = commandParams[1]
    if commandAction == 'SUM':
        # ### 2.3 - print
        print(doSum(rows))
    elif commandAction == 'AVG':
        print(doAvg(rows))